package com.webwork.lims.warehouse.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.webwork.lims.common.controller.SuperCtrl;
import com.webwork.lims.common.dataobject.CtrlerAndTime;
import com.webwork.lims.stock.dataobject.Product;
import com.webwork.lims.stock.service.ProductService;
import com.webwork.lims.stock.service.StockService;
import com.webwork.lims.system.dataobject.User;
import com.webwork.lims.util.DateFormat;
import com.webwork.lims.util.Paging;
import com.webwork.lims.util.StrManager;
import com.webwork.lims.warehouse.dataobject.IncomeOrder;
import com.webwork.lims.warehouse.dataobject.WarehouseStatus;
import com.webwork.lims.warehouse.service.IncomeService;

@Controller
@RequestMapping(value = "/warehouse")
public class IncomeController extends SuperCtrl {

	@Autowired
	private IncomeService incomeSve;
	
	@Autowired
	private StockService stockSve;
	
	@Autowired
	private ProductService productSve;
	
	@RequestMapping(value = "/income")
	@Override
	public String mainPage() {
		return "income";
	}
	
	@RequestMapping(value = "queryIncomeList")
	@ResponseBody
	public ResponseEntity<String> queryList(
			@RequestParam(value = "page") int page,
			@RequestParam(value = "rows") int rows,
			@RequestParam(required = false, value = "cmd") String cmdStr)
					throws Exception {
		IncomeOrder queryObj = new IncomeOrder();
		if (!StrManager.isEmpty(cmdStr)) {
			queryObj = new Gson().fromJson(cmdStr, IncomeOrder.class);
			queryObj.setStatus(queryObj.getStatusCode());
		}
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
	
		ret.put("total", incomeSve.count(queryObj));
		ret.put("rows", incomeSve.query(queryObj, new Paging(page, rows)));
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "getIncome")
	@ResponseBody
	public ResponseEntity<String> getIncome(@RequestParam("incomeId") String incomeId) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		IncomeOrder order = incomeSve.get(incomeId);
		return new ResponseEntity<String>(new Gson().toJson(order).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value="loadStatus")
	@ResponseBody
	public ResponseEntity<String> loadStatus() {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		WarehouseStatus[] status = WarehouseStatus.useableValues();
		List<Object> ret = new ArrayList<Object>();
		for (WarehouseStatus o : status) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("value", o.getCode());
			map.put("name", o.getCode() + " - " + o.getMsg());
			ret.add(map);
		}
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "addIncome")
	@ResponseBody
	public String addIncome(@RequestParam("jsonObj") String jsonObj, HttpServletRequest request) {
		User user = (User) request.getSession(true).getAttribute("user");
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		IncomeOrder order = new Gson().fromJson(jsonObj, IncomeOrder.class);
		order.setIncomeId("IC" + DateFormat.NUMBER.format(new Date()));
		order.setStatus(WarehouseStatus.CREATED);
		order.setCreate(new CtrlerAndTime(user.getUserName(), new Date()));
		incomeSve.save(order);
		return SUCCESS;
	}
	
	@RequestMapping(value = "editIncome")
	@ResponseBody
	public String editIncome(@RequestParam("jsonObj") String jsonObj) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		IncomeOrder o = new Gson().fromJson(jsonObj, IncomeOrder.class);
		IncomeOrder order = incomeSve.get(o.getIncomeId());
		if (order == null) {
			return NOT_EXIST;
		}
		order.setSku(o.getSku());
		order.setAmount(o.getAmount());
		incomeSve.save(order);
		return SUCCESS;
	}
	
	@RequestMapping(value = "incomeOrder")
	@ResponseBody
	public String incomeOrder(@RequestParam("incomeId") String incomeId, HttpServletRequest request) {
		User user = (User) request.getSession(true).getAttribute("user");
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		IncomeOrder order = incomeSve.get(incomeId);
		if (order == null) {
			return FAIL;
		}
		
		if (productSve.get(order.getSku()) == null) {
			Product product = new Product();
			product.setSku(order.getSku());
			productSve.insert(null);
		}
		
		stockSve.addStock(order.getSku(), order.getAmount());
		
		order.setStatus(WarehouseStatus.COMPLETE);
		order.setComplete(new CtrlerAndTime(user.getUserName(), new Date()));
		incomeSve.save(order);
		
		return SUCCESS;
	}
	
	@RequestMapping(value = "cancelIncome")
	@ResponseBody
	public String cancelOrder(@RequestParam("incomeId") String incomeId, HttpServletRequest request) {
		User user = (User) request.getSession(true).getAttribute("user");
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		IncomeOrder order = incomeSve.get(incomeId);
		if (order == null) {
			return FAIL;
		}
		order.setStatus(WarehouseStatus.CANCEL);
		order.setComplete(new CtrlerAndTime(user.getUserName(), new Date()));
		incomeSve.save(order);
		return SUCCESS;
	}
	
}
